<script setup lang="ts">
import AddWrite from './components/addWrite.vue'
import { ContentDetailWrap } from '@/components/ContentDetailWrap'
import { ref, unref } from 'vue'
import { ElButton } from 'element-plus'
import { useI18n } from '@/hooks/web/useI18n'
import { useRouter } from 'vue-router'
import { clientAdd } from '@/api/common'
import { customerManagementType } from '@/api/common/types'
import { useEmitt } from '@/hooks/web/useEmitt'

const { emitter } = useEmitt()

const { push } = useRouter()

const { t } = useI18n()

const addWrite = ref<ComponentRef<typeof AddWrite>>()

const loading = ref(false)

const save = async () => {
  const write = unref(addWrite)
  console.log('---addWrite---', write?.getFormData())

  const data = {
    code: write?.getFormData()?.code,
    fahuo: write?.getFormData()?.fahuo,
    shouhuo: write?.getFormData()?.shouhuo,
    shmobile: write?.getFormData()?.shmobile,
    passport: write?.getFormData()?.passport,
    email: write?.getFormData()?.email,
    salesman: write?.getFormData()?.salesman,
    status: write?.getFormData()?.status,
    assess: write?.getFormData()?.assess,
    placeend: write?.getFormData()?.placeend,
    submarket: write?.getFormData()?.submarket,
    looktime: write?.getFormData()?.looktime,
    restrict: write?.getFormData()?.restrict,
    username: write?.getFormData()?.username,
    password: write?.getFormData()?.password
  } as customerManagementType
  // data.buytime = '' + parseInt(data.buytime) / 1000
  console.log('---客户新增参数---', data)
  const res = await clientAdd(data)
  if (res) {
    emitter.emit('getList', 'add')
    push('/personnelManagement/customerManagement')
  }
}
</script>

<template>
  <ContentDetailWrap
    :title="t('exampleDemo.add')"
    @back="push('/personnelManagement/customerManagement')"
  >
    <!-- <Write ref="writeRef" /> -->
    <AddWrite ref="addWrite" />

    <template #right>
      <ElButton type="primary" :loading="loading" @click="save">
        {{ t('exampleDemo.save') }}
      </ElButton>
    </template>
  </ContentDetailWrap>
</template>
